/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.edu.pucp.Seguridad.Vista;

import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;
import pe.edu.pucp.Modelo.Ciudad;
import pe.edu.pucp.Modelo.Continente;
import pe.edu.pucp.Modelo.EstadoUsuario;
import pe.edu.pucp.Modelo.Pais;
import pe.edu.pucp.Modelo.Permiso;
import pe.edu.pucp.Modelo.Usuario;
import pe.edu.pucp.Util.Constante;
import pe.edu.pucp.Util.ValHelper;

public class BuscarUsuarioGUI extends javax.swing.JPanel {

    private JFrame padre;
    
    public BuscarUsuarioGUI() {
        initComponents();        
    }   
    
    public JFrame getPadre(){ return padre; }
    
    public String getNombre(){ return txtNombre.getText().trim().toUpperCase(); } 
    
    public int getCiudad(){
        if (cmbCiudad.getSelectedIndex() > 0) {
            return cmbCiudad.getSelectedIndex() - 1;
        }
        else {
            return -1;
        }        
    } 
    
    public int getPais(){ 
        if(cmbPais.getSelectedIndex() > 0) {
            return cmbPais.getSelectedIndex() - 1;
        }
        else {
            return -1;
        }
    }     
    
    public int getContinente(){ 
        if (cmbContinente.getSelectedIndex() > 0) {
            return cmbContinente.getSelectedIndex() - 1;
        }
        else {
            return -1;
        }     
    }  
    
    public int getEstado(){
        if (cmbEstado.getSelectedIndex() > 0) {
            return cmbEstado.getSelectedIndex() - 1;
        } 
        else {
            return -1;
        }     
    }      
        
    public int getFilaSel(){ return tblResultados.getSelectedRow(); }     

    public void mostrarPermisos(ArrayList<Permiso> list){            
        if (!list.isEmpty()){                                
            btnBuscar.setEnabled(buscarPermiso(list,Constante.P_SEG_USR_BUSCAR)==1);
            btnActivar.setEnabled(buscarPermiso(list,Constante.P_SEG_USR_ACTIVAR)==1);
            btnEliminar.setEnabled(buscarPermiso(list,Constante.P_SEG_USR_INACTIVAR)==1);
            btnCrear.setEnabled(buscarPermiso(list,Constante.P_SEG_USR_REGISTRAR)==1);
            btnEditar.setEnabled(buscarPermiso(list,Constante.P_SEG_USR_EDITAR)==1);
        }
    }    
  
    public int buscarPermiso(ArrayList<Permiso> list, int idel){
        int per=-1;
        for (Permiso opc : list){
            if (opc.getElemento().getIdelemento()==idel){ per =opc.getPermiso(); }
        }
        return per;
    }     
          
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        btnLimpiar = new javax.swing.JButton();
        btnBuscar = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        cmbEstado = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        cmbContinente = new javax.swing.JComboBox();
        cmbPais = new javax.swing.JComboBox();
        cmbCiudad = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        btnCrear = new javax.swing.JButton();
        btnEditar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        btnActivar = new javax.swing.JButton();
        pnUsuarios = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblResultados = new javax.swing.JTable();

        setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        setMaximumSize(new java.awt.Dimension(764, 700));
        setMinimumSize(new java.awt.Dimension(764, 700));
        setPreferredSize(new java.awt.Dimension(764, 700));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Buscar", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Ubuntu", 0, 15))); // NOI18N

        jLabel1.setText("Nombre:");

        btnLimpiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recurso/refresh.png"))); // NOI18N
        btnLimpiar.setToolTipText("Limpiar");

        btnBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recurso/search.png"))); // NOI18N
        btnBuscar.setToolTipText("Buscar");

        jLabel3.setText("Estado");

        jLabel4.setText("Continente");

        jLabel7.setText("País");

        jLabel8.setText("Ciudad");

        btnCrear.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recurso/plus.png"))); // NOI18N
        btnCrear.setToolTipText("Nuevo");

        btnEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recurso/edit.png"))); // NOI18N
        btnEditar.setToolTipText("Editar");
        btnEditar.setMaximumSize(new java.awt.Dimension(57, 29));
        btnEditar.setMinimumSize(new java.awt.Dimension(57, 29));
        btnEditar.setPreferredSize(new java.awt.Dimension(57, 29));

        btnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recurso/down_arrow.png"))); // NOI18N
        btnEliminar.setToolTipText("Inactivar");

        btnActivar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recurso/up_arrow.png"))); // NOI18N
        btnActivar.setToolTipText("Activar");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(jLabel4)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 368, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(cmbPais, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(cmbContinente, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel8))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(cmbCiudad, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(cmbEstado, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(0, 17, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btnCrear, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnActivar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(cmbContinente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(jLabel7)
                            .addComponent(cmbPais, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(35, 35, 35)
                        .addComponent(jLabel3))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(cmbCiudad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmbEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(62, 62, 62)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnActivar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCrear, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(btnLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pnUsuarios.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Resultados", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Ubuntu", 0, 15))); // NOI18N

        tblResultados.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "NOMBRE", "PERFIL", "FECHA REGISTRO", "ESTADO"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblResultados.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblResultados.getTableHeader().setResizingAllowed(false);
        tblResultados.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(tblResultados);
        tblResultados.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblResultados.getColumnModel().getColumn(0).setResizable(false);
        tblResultados.getColumnModel().getColumn(1).setResizable(false);
        tblResultados.getColumnModel().getColumn(2).setResizable(false);
        tblResultados.getColumnModel().getColumn(3).setResizable(false);

        javax.swing.GroupLayout pnUsuariosLayout = new javax.swing.GroupLayout(pnUsuarios);
        pnUsuarios.setLayout(pnUsuariosLayout);
        pnUsuariosLayout.setHorizontalGroup(
            pnUsuariosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnUsuariosLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 700, Short.MAX_VALUE)
                .addContainerGap())
        );
        pnUsuariosLayout.setVerticalGroup(
            pnUsuariosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnUsuariosLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 356, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pnUsuarios, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnUsuarios, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(35, 35, 35))
        );
    }// </editor-fold>//GEN-END:initComponents

    public void addNuevoListener(ActionListener ml){ this.btnCrear.addActionListener(ml); }
    
    public void addBuscarListener(ActionListener ml){ this.btnBuscar.addActionListener(ml); }    

    public void addLimpiarListener(ActionListener ml){ this.btnLimpiar.addActionListener(ml); }  

    public void addELiminarListener(ActionListener ml){ this.btnEliminar.addActionListener(ml); }     
    
    public void addActivarListener(ActionListener ml){ this.btnActivar.addActionListener(ml); } 
    
    public void addEditarListener(ActionListener ml){ this.btnEditar.addActionListener(ml); }          

    public void addCambiarContinente(ItemListener ml){ this.cmbContinente.addItemListener(ml); }     
    
    public void addCambiarPais(ItemListener ml){ this.cmbPais.addItemListener(ml); }         
    
    public void addCambiarCiudad(ItemListener ml){ this.cmbCiudad.addItemListener(ml); }              
            
    public void llenaComboPais(ArrayList<Pais> lista){
        cmbPais.removeAllItems();
        cmbPais.addItem(Constante.DEFAULT_OPCION);                
        if(lista!=null){           
            for (Pais opc : lista){
                cmbPais.addItem(opc.getDescripcion());
            }
        }
    }    
    
    public void llenaComboContinente(ArrayList<Continente> lista){
        cmbContinente.removeAllItems();
        cmbContinente.addItem(Constante.DEFAULT_OPCION); 
        if(lista!=null){
            for (Continente opc : lista){
                cmbContinente.addItem(opc.getDescripcion());
            }
        }
    }

    public void llenaComboCiudad(ArrayList<Ciudad> lista){
        cmbCiudad.removeAllItems();
        cmbCiudad.addItem(Constante.DEFAULT_OPCION); 
        if(lista!=null){
            for (Ciudad opc : lista){
                cmbCiudad.addItem(opc.getDescripcion());
            }
        }
    }     
     
    public void llenaComboEstado(ArrayList<EstadoUsuario> lista){
        cmbEstado.removeAllItems();
        cmbEstado.addItem(Constante.DEFAULT_OPCION);         
        if(lista!=null){
            for (EstadoUsuario opc : lista){
                cmbEstado.addItem(opc.getDescripcion());
            }
        }
    }     
    
    public void llenaGrilla(ArrayList<Usuario> lista){           
        if(lista!=null){ 
            ((DefaultTableModel) this.tblResultados.getModel()).setRowCount(0);
            for (int i = 0; i<lista.size();i++){
                ((DefaultTableModel) tblResultados.getModel()).setRowCount(tblResultados.getRowCount()+1);
                tblResultados.setValueAt(lista.get(i).getNombre()+" "+lista.get(i).getAppaterno(),i,0);
                tblResultados.setValueAt(lista.get(i).getPerfil().getDescripcion(),i,1);
                tblResultados.setValueAt(ValHelper.fechaMenu(lista.get(i).getFechareg()),i,2);         
                tblResultados.setValueAt(lista.get(i).getEstadoUsuario().getDescripcion(),i,3);                         
             }             
             pnUsuarios.setBorder(javax.swing.BorderFactory.createTitledBorder("Usuarios ("+lista.size() + ")"));
        }
        else {
            pnUsuarios.setBorder(javax.swing.BorderFactory.createTitledBorder("Usuarios"));
        }        
    }     
    
    public void LimpiarDatos(){
        txtNombre.setText("");
        cmbContinente.setSelectedIndex(0);
        cmbPais.setSelectedIndex(0);
        cmbCiudad.setSelectedIndex(0);
        cmbEstado.setSelectedIndex(0);    
        ((DefaultTableModel) this.tblResultados.getModel()).setRowCount(0);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnActivar;
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnCrear;
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnLimpiar;
    private javax.swing.JComboBox cmbCiudad;
    private javax.swing.JComboBox cmbContinente;
    private javax.swing.JComboBox cmbEstado;
    private javax.swing.JComboBox cmbPais;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel pnUsuarios;
    private javax.swing.JTable tblResultados;
    private javax.swing.JTextField txtNombre;
    // End of variables declaration//GEN-END:variables
}
